ZONOTES
主页
网址
笔记
博客
关于
拒绝内耗,先完成,再完美
Powered |
VitePress
黔ICP备-2023015500号-3
⏰2025年03月19日
修改中
pinia
25031901

Vitepress中使用 pinia

官网https://pinia.vuejs.org/zh/

1. 安装依赖

shell
npm install pinia
shell
yarn add pinia
shell
pnpm install pinia

2. 配置代码

.vitepress/theme/index.js 中设置 Pinia:

js
import { createPinia } from 'pinia'
import DefaultTheme from 'vitepress/theme'

export default {
  extends: DefaultTheme,
  enhanceApp({ app }) {
    const pinia = createPinia()
    app.use(pinia)
  }
}

3. 创建 store

.vitepress/stores/counter.js 中创建示例 store:

js
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++
    }
  }
})

4. 组件中使用

vue
<script setup>
import { useCounterStore } from '../.vitepress/stores/counter'
const counter = useCounterStore()
</script>

<template>
  <div>
    <p>Count: {{ counter.count }}</p>
    <button @click="counter.increment()">+1</button>
  </div>
</template>

5. 持久化

  • 安装持久化插件
shell
pnpm install pinia pinia-plugin-persistedstate